home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / gcc / ixemlsrc.lha / ixemul / ixnet / updutils.c < prev    next >
C/C++ Source or Header  |  1996-03-13  |  5KB  |  178 lines

  1. /*-
  2.  * Copyright (c) 1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Mike Olson.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #if defined(LIBC_SCCS) && !defined(lint)
  38. static char sccsid[] = "@(#)updutils.c    5.2 (Berkeley) 2/22/91";
  39. #endif /* LIBC_SCCS and not lint */
  40.  
  41. #include <sys/types.h>
  42. #include <db.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #include "btree.h"
  46. #include "utils.h"
  47.  
  48. int _bt_crsrkey(BTREE_P);
  49.  
  50. /*
  51.  *  _BT_FIXSCAN -- Adjust a scan to cope with a change in tree structure.
  52.  *
  53.  *    If the user has an active scan on the database, and we delete an
  54.  *    item from the page the cursor is pointing at, we need to figure
  55.  *    out what to do about it.  Basically, the solution is to point
  56.  *    "between" keys in the tree, using the CRSR_BEFORE flag.  The
  57.  *    requirement is that the user should not miss any data in the
  58.  *    tree during a scan, just because he happened to do some deletions
  59.  *    or insertions while it was active.
  60.  *
  61.  *    In order to guarantee that the scan progresses properly, we need
  62.  *    to save the key of any deleted item we were pointing at, so that
  63.  *    we can check later insertions against it.
  64.  *
  65.  *    Parameters:
  66.  *        t -- tree to adjust
  67.  *        index -- index of item at which change was made
  68.  *        newd -- new datum (for insertions only)
  69.  *        op -- operation (DELETE or INSERT) causing change
  70.  *
  71.  *    Returns:
  72.  *        RET_SUCCESS, RET_ERROR (errno is set).
  73.  *
  74.  *    Side Effects:
  75.  *        None.
  76.  */
  77.  
  78. int
  79. _bt_fixscan(t, index, newd, op)
  80.     BTREE_P t;
  81.     index_t index;
  82.     DATUM *newd;
  83.     int op;
  84. {
  85.     CURSOR *c;
  86.     DATUM *d;
  87.  
  88.     c = &(t->bt_cursor);
  89.  
  90.     if (op == DELETE) {
  91.         if (index < c->c_index)
  92.             c->c_index--;
  93.         else if (index == c->c_index) {
  94.             if (!(c->c_flags & CRSR_BEFORE)) {
  95.                 if (_bt_crsrkey(t) == RET_ERROR)
  96.                     return (RET_ERROR);
  97.                 c->c_flags |= CRSR_BEFORE;
  98.             }
  99.         }
  100.     } else {
  101.         /*
  102.          *  If we previously deleted the object at this location,
  103.          *  and now we're inserting a new one, we need to do the
  104.          *  right thing -- the cursor should come either before
  105.          *  or after the new item, depending on the key that was
  106.          *  here, and the new one.
  107.          */
  108.  
  109.         if (c->c_flags & CRSR_BEFORE) {
  110.             if (index <= c->c_index) {
  111.                 char *tmp;
  112.                 int itmp;
  113.                 pgno_t chain;
  114.                 int r;
  115.  
  116.                 d = (DATUM *) GETDATUM(t->bt_curpage, index);
  117.                 if (d->d_flags & D_BIGKEY) {
  118.                     bcopy(&(newd->d_bytes[0]),
  119.                           (char *) &chain,
  120.                           sizeof(chain));
  121.                     if (_bt_getbig(t, chain, &tmp, &itmp)
  122.                          == RET_ERROR)
  123.                         return (RET_ERROR);
  124.                 } else
  125.                     tmp = &(newd->d_bytes[0]);
  126.  
  127.                 r = (*(t->bt_compare))(tmp, c->c_key);
  128.                 if (r < 0)
  129.                     c->c_index++;
  130.  
  131.                 if (d->d_flags & D_BIGKEY)
  132.                     (void) free (tmp);
  133.             }
  134.         } else if (index <= c->c_index)
  135.             c->c_index++;
  136.     }
  137.     return (RET_SUCCESS);
  138. }
  139.  
  140. /*
  141.  *  _BT_CRSRKEY -- Save a copy of the key of the record that the cursor
  142.  *           is pointing to.  The record is about to be deleted.
  143.  *
  144.  *    Parameters:
  145.  *        t -- btree
  146.  *
  147.  *    Returns:
  148.  *        RET_SUCCESS, RET_ERROR.
  149.  *
  150.  *    Side Effects:
  151.  *        Allocates memory for the copy which should be released when
  152.  *        it is no longer needed.
  153.  */
  154.  
  155. int
  156. _bt_crsrkey(t)
  157.     BTREE_P t;
  158. {
  159.     CURSOR *c;
  160.     DATUM *d;
  161.     pgno_t pgno;
  162.     int ignore;
  163.  
  164.     c = &(t->bt_cursor);
  165.     d = (DATUM *) GETDATUM(t->bt_curpage, c->c_index);
  166.  
  167.     if (d->d_flags & D_BIGKEY) {
  168.         bcopy(&(d->d_bytes[0]), (char *) &pgno, sizeof(pgno));
  169.         return (_bt_getbig(t, pgno, &(c->c_key), &ignore));
  170.     } else {
  171.         if ((c->c_key = (char *) malloc(d->d_ksize)) == (char *) NULL)
  172.             return (RET_ERROR);
  173.  
  174.         bcopy(&(d->d_bytes[0]), c->c_key, (size_t) (d->d_ksize));
  175.     }
  176.     return (RET_SUCCESS);
  177. }
  178.